//
//  OpenLockHistVC.m
//  AILock
//
//  Created by 谭亚明 on 2020/7/13.
//  Copyright © 2020 tym. All rights reserved.
//

#import "UnusualPoliceVC.h"
#import "MessageHistoryCell.h"
#import "TYMBaseTableView.h"
#import "UIView+ZFFrame.h"
#import "CustomGifFooter.h"
#import "CustomGifHeader.h"
#import "EquipmentService.h"

@interface UnusualPoliceVC ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,weak) IBOutlet TYMBaseTableView *tableView;

@property(nonatomic,strong) EquipmentService *services;

@property(nonatomic,strong) NSMutableArray *sectionTitleArray;
@property(nonatomic,strong) NSMutableDictionary *dataDic;

@property(nonatomic,strong) EquipmentModel *equipModel;

@property(nonatomic,assign) NSInteger pageIndex;
@end

@implementation UnusualPoliceVC

-(instancetype)initWithModel:(EquipmentModel*)model{
    self = [super init];
    self.equipModel = model;
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"MessageHistoryCell" bundle:nil] forCellReuseIdentifier:[MessageHistoryCell identifier]];
    self.tableView.zf_width = ScreenWidth;
    self.tableView.layer.cornerRadius = 20;
    self.tableView.layer.masksToBounds = YES;
    self.pageIndex = 1;
    
    self.tableView.mj_header = [CustomGifHeader headerWithRefreshingBlock:^{
        self.pageIndex = 1;
        [self getOpenLockHistoryList];
    }];
    
    self.tableView.mj_footer = [CustomGifFooter footerWithRefreshingBlock:^{
        self.pageIndex += 1;
        [self getOpenLockHistoryList];
    }];
    [self getOpenLockHistoryList];
}

-(NSMutableDictionary*)dataDic{
    if (_dataDic == nil) {
        _dataDic = [[NSMutableDictionary alloc] init];
    }
    return _dataDic;
}

-(NSMutableArray*)sectionTitleArray{
    if (_sectionTitleArray == nil) {
        _sectionTitleArray = [[NSMutableArray alloc] init];
    }
    return _sectionTitleArray;
}

-(void)getOpenLockHistoryList{
    TYMWeak(self);
    [self.services getFindequipmentAlarmEquipmentID:self.equipModel.equipmentId page:self.pageIndex completion:^(LMJBaseResponse * _Nonnull respons, NSArray<EquipmentMessageModel *> * _Nonnull repair) {
        [weakself.tableView.mj_header endRefreshing];
        [weakself.tableView.mj_footer endRefreshing];
        if (self.pageIndex == 1) {
            [self.dataDic removeAllObjects];
            [self.sectionTitleArray removeAllObjects];
        }
        for (EquipmentMessageModel *model in repair) {
            if (!model.createTime) {
                continue;
            }
            if (![weakself.sectionTitleArray containsObject:[model.createTime substringToIndex:10]]) {
                
                [weakself.sectionTitleArray addObject:[model.createTime substringToIndex:10]];
                
                NSMutableArray *array = [[NSMutableArray alloc] init];
                [array addObject:model];
                [weakself.dataDic setValue:array forKey:[model.createTime substringToIndex:10]];
            }else{
                NSMutableArray *array = weakself.dataDic[[model.createTime substringToIndex:10]];
                [array addObject:model];
                [weakself.dataDic setValue:array forKey:[model.createTime substringToIndex:10]];
            }
        }
        if (repair.count < 10) {
            [weakself.tableView.mj_footer endRefreshingWithNoMoreData];
        }else{
            [weakself.tableView.mj_footer resetNoMoreData];
        }
        [weakself.tableView reloadData];
    }];
}

-(EquipmentService*)services{
    if (_services == nil) {
        _services = [[EquipmentService alloc] init];
    }
    return _services;
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
//表格组数 Sections 组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.sectionTitleArray.count;
}
//每组返回行数 Rows 行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSMutableArray *array = self.dataDic[self.sectionTitleArray[section]];
    return array.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [MessageHistoryCell cellForHeight];
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 44)];
    headView.backgroundColor = [UIColor whiteColor];
    TYMLabel *yearLab = [TYMLabel lb_text:[NSString stringWithFormat:@"%@年",[self.sectionTitleArray[section] substringToIndex:4]] font:Font_Regular(12) color:rgba(155, 155, 155)];
    yearLab.frame = CGRectMake(15, 13.5, 45, 16.5);
    [headView addSubview:yearLab];

    TYMLabel *mdLab = [TYMLabel lb_text:[NSString stringWithFormat:@"%@",[self.sectionTitleArray[section] substringWithRange:NSMakeRange(5, 5)]] font:Font_Regular(15) color:rgba(74, 74, 74)];
    mdLab.frame = CGRectMake(62, 11, 54, 21);
    [headView addSubview:mdLab];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 43, ScreenWidth - 30, 1)];
    lineView.backgroundColor = rgba(240, 240, 240);
    [headView addSubview:lineView];
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}

//每个单元格的e内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MessageHistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:[MessageHistoryCell identifier]];
    
    NSMutableArray *array = self.dataDic[self.sectionTitleArray[indexPath.section]];
    cell.messageModel = array[indexPath.row];
    return cell;
}
@end
